package com.atguigu.guli.service.oss.controller;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.atguigu.guli.service.base.result.R;
import com.atguigu.guli.service.oss.config.OssProperties;
import com.atguigu.guli.service.oss.service.FileService;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.PostConstruct;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

@CrossOrigin
@RestController
@RequestMapping("/admin/oss/file")
public class FileController {
    @Autowired
    FileService fileService;


    //1、处理文件上传
    @PostMapping("upload")
    //参数1：  上传的文件表单项   参数2： oss保存文件的目录(模块)
    public R upload(MultipartFile file , String module){
        String path = fileService.upload(file , module);
        //返回上传成功的图片地址
        return R.ok().data("path" , path);
    }
    //2、处理文件的删除
    @DeleteMapping("delete")
    public R deleteFile(@RequestParam("path") String path ,
                        @RequestParam("module") String module){
//        try {
//            TimeUnit.SECONDS.sleep(2);
//        } catch (InterruptedException e) {
//            e.printStackTrace();
//        }
        fileService.delete(path , module);
        return R.ok();
    }
}
